package com.ky.gp.app.controller;


import com.ky.common.param.ArticleAdd;
import com.ky.common.vo.R;
import com.ky.gp.app.service.ArticleFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 * 文章表 前端控制器
 * </p>
 *
 * @author wgz
 * @since 2024-06-15
 */
@RestController
@RequestMapping("/api/study")
public class ArticleController {
    @Autowired
    ArticleFeign articleFeign;

    @RequestMapping("/article/all")
    public R all() {
        return R.ok(articleFeign.all());
    }

    @RequestMapping("/article/search")
    public R search(@RequestParam("keyword")String keyword) {
        return R.ok(articleFeign.search(keyword));
    }

    @RequestMapping("/article/bkjy")
    public R bkjy() {
        return R.ok(articleFeign.bkjy());
    }

    @RequestMapping("/article/alfx")
    public R alfx() {
        return R.ok(articleFeign.alfx());
    }

    @RequestMapping("/article/kt")
    public R kt() {
        return R.ok(articleFeign.kt());
    }

    @RequestMapping("/article/jzs")
    public R jzs() {
        return R.ok(articleFeign.jzs());
    }

    @RequestMapping("/article/js")
    public R js() {
        return R.ok(articleFeign.js());
    }

    @RequestMapping("/article/selectByArticleId/{articleId}")
    public R selectByArticleId(@PathVariable int articleId) {
        return R.ok(articleFeign.selectByArticleId(articleId));
    }

    @RequestMapping("/article/addViews/{articleId}")
    public R addViews(@PathVariable int articleId) {
        return R.ok(articleFeign.addViews(articleId));
    }

    // 点赞
    @RequestMapping("/article/goods/{articleId}")
    public R addGoods(@PathVariable int articleId) {
        return R.ok(articleFeign.addGoods(articleId));
    }

    // 发起请求http://localhost:9091/study/article/goods/${articleId}/goods来更新点赞数
    @RequestMapping("/article/goods/{articleId}/goods")
    public R addGoods2(@PathVariable int articleId) {
        return R.ok(articleFeign.addGoods2(articleId));
    }

    @RequestMapping("/article/commentsCount/{articleId}/commentsCount")
    public R commentsCount(@PathVariable int articleId) {
        return R.ok(articleFeign.commentsCount(articleId));
    }

    @RequestMapping("/article/addArticle")
    public R addArticle( @RequestBody ArticleAdd articleAdd) {
       return R.ok( articleFeign.addArticle(articleAdd));
    }
    @PostMapping("/article/upload")
    public R uploadFile(@RequestPart("file") MultipartFile file) throws IOException {

        // MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        // HttpHeaders fileHeaders = new HttpHeaders();
        // fileHeaders.setContentType(MediaType.parseMediaType(file.getContentType()));
        // HttpEntity<byte[]> fileEntity = new HttpEntity<>(file.getBytes(), fileHeaders);
        // body.add("file", fileEntity);
        //
        // // // 调用目标服务的上传接口
        // // String targetUrl = "http://target-service-url/upload"; // 目标服务地址
        // // ResponseEntity<String> response = restTemplate.postForEntity(targetUrl, body, String.class);
        // //
        // // return response;

        return articleFeign.uploadFile(file);
    }


}